<!DOCTYPE html>
<title>Test autoplay muted behaviour in various system conditions.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<body></body>
<script>
var tests = [
  {
    name: 'regular conditions',
    conditions: {
      dataSaver: false,
      cellular: false,
    },
    expectations: {
      autoplay: true
    }
  },
  {
    name: 'cellular connections',
    conditions: {
      dataSaver: false,
      cellular: true,
    },
    expectations: {
      autoplay: true
    }
  },
  {
    name: 'cellular connections with Data Saver',
    conditions: {
      dataSaver: true,
      cellular: true,
    },
    expectations: {
      autoplay: true
    }
  },
  {
    name: 'Data Saver',
    conditions: {
      dataSaver: true,
      cellular: false,
    },
    expectations: {
      autoplay: true
    }
  },
];

var asyncTests = tests.map(test => async_test('Testing autoplay behaviour for ' + test.name));

var currentTest = 0;
function runNextTest() {
  asyncTests[currentTest].done();

  ++currentTest;
  if (currentTest == tests.length)
    return;

  asyncTests[currentTest].step_func(runTest(asyncTests[currentTest], tests[currentTest]));
}

function runTest(t, test) {
  // Sanity checks for 'conditions'.
  assert_true('dataSaver' in test.conditions);
  assert_true('cellular' in test.conditions);

  internals.setSaveDataEnabled(test.conditions.dataSaver);
  if (test.conditions.cellular)
    internals.setNetworkConnectionInfoOverride(true, 'cellular3g', '4g', 100, 2.0);
  else
    internals.setNetworkConnectionInfoOverride(true, 'wifi', '4g', 100, 2.0);

  // Generic required settings.
  internals.settings.setAutoplayPolicy('user-gesture-required');
  internals.runtimeFlags.autoplayMutedVideosEnabled = true;

  t.add_cleanup(() => {
    internals.setSaveDataEnabled(false);
    internals.clearNetworkConnectionInfoOverride();
    internals.settings.setAutoplayPolicy('no-user-gesture-required');
    internals.runtimeFlags.autoplayMutedVideosEnabled = false;
  });

  var count = 0;
  [ 'attribute', 'method' ].forEach(type => {
    var media = document.createElement('video');
    document.body.appendChild(media);

    var expectedEvents = [];
    if (test.expectations.autoplay)
      expectedEvents = [ 'play', 'playing' ];
    else
      expectedEvents = [ 'suspend' ];

    var eventWatcher = new EventWatcher(t, media, expectedEvents);
    eventWatcher.wait_for(expectedEvents).then(_ => {
      if (test.expectations.autoplay) {
        assert_equals(media.readyState, media.HAVE_ENOUGH_DATA);
        assert_false(media.paused);
      } else {
        assert_true(media.paused);
      }

      document.body.removeChild(media);
      ++count;
      if (count == 2)
        runNextTest();
    });

    media.muted = true;
    media.src = 'content/test.ogv';

    if (type == 'attribute')
      media.autoplay = true;
    else if (type == 'method')
      media.play().catch(e => {});
  });
}

asyncTests[currentTest].step_func(runTest(asyncTests[currentTest], tests[currentTest]));

</script>
